<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use app\portal\model\PortalCategoryModel;
use app\portal\service\PostService;
use app\portal\model\PortalPostModel;
use think\Db;

class ListController extends HomeBaseController
{
    public function index()
    {
        $img = Db::name('img')->find(2);
        $this->assign('img',$img);

        $parent_id = input('id', 0, 'intval');

        $portalCategoryModel = new PortalCategoryModel();
        $category            = $portalCategoryModel->where('parent_id', $parent_id)->select();

        foreach ($category as $k => $v) {
            $category[$k]['sub_cate'] = $portalCategoryModel->where('parent_id', $v['id'])->select();

            foreach ($category[$k]['sub_cate'] as $kk => $vv) {
                $category[$k]['sub_cate'][$kk]['lists'] = cmf_sql_posts_bycatid($vv['id'],"field:post_title,post_id,post.more,category_id,published_time,post_excerpt;order:post_id DESC;",'');
            }
        }
       
        $listTpl = empty($category['list_tpl']) ? 'list' : $category['list_tpl'];
        // pre($listTpl,1);die;

        // $this->assign('cat_id', $id);
        $this->assign('category', $category);

        // pre($category->toArray());die;
        
        $request = request();


        if( $request->isAjax() ){
            $html = $this->fetch($listTpl.'_ajax');
            
            $return = array(
                // 'error'     => 0,
                'data'      => $html,
                // 'start'     => $start,
                // 'size'      => $size,
                // 'more_data' => empty($more_data) ? 0 : 1 ,
            );
            return $return;

        }

        return $this->fetch('/' . $listTpl);
    }

    //ajax加载更多，查询该分类及其所有子分类下的文章
    public function listAjaxByCatId(){
        
        $cid      = input('cid',0,'intval');
        $start    = input('start',0,'intval');
        $size     = input('size',0,'intval');
        $list_tpl = input('list_tpl','','trim');
        $recomm   = input('recomm',0,'intval');

        $where = array();
        if( $recomm ){
            $where['recommended'] = 1;
        }

        $limit = $start.','.$size;
        $lists = cmf_sql_posts_bycatid($cid,"field:post_title,post_id,post.more,category_id,published_time,post_excerpt;".TMPLSO."limit:$limit;",$where);

            
        if( count($lists) < 1 ){
            $return = array(
                'error'   => 1,
            );
            return $return;
        }


        $this->assign('lists',$lists);

        //使用的模版
        $html = $this->fetch($list_tpl.'_ajax');

        //是否存在更多数据
        $more_limit = $start+$size.',1';
        $more_data = cmf_sql_posts_bycatid($cid,TMPLSO."limit:$more_limit;",$where);


        $return = array(
            'error'     => 0,
            'data'      => $html,
            'start'     => $start,
            'size'      => $size,
            'more_data' => (count($more_data) < 1) ? 0 : 1 ,
        );
        return $return;
    }

    //ajax加载更多，查询该分类下的文章
    public function listAjax(){
        $cid      = input('cid',0,'intval');
        $start    = input('start',0,'intval');
        $size     = input('size',0,'intval');
        $list_tpl = input('list_tpl','','trim');

        $limit = $start.','.$size;
        $lists = cmf_sql_posts("category_ids:$cid;field:post_id,category_id,post_title,post_source,post_excerpt,post.more,file_url,published_time;limit:$limit;".TMPLSO);
        // pre($lists,1);

        //没有数据
        if( count($lists) < 1 ){
            $return = array(
                'error'   => 1,
            );
            return $return;
        }
        
        if( !empty($list_tpl) ){
            //赋值，使用的模版
            $this->assign('lists',$lists);
            $html = $this->fetch($list_tpl.'_ajax');
        }

        //是否存在更多数据
        $more_limit = $start+$size.',1';
        $more_data = cmf_sql_posts("category_ids:$cid;field:post_id;limit:$more_limit;".TMPLSO);

        $return = array(
            'error'     => 0,
            'data'      => empty($list_tpl) ? $lists : $html ,
            'start'     => $start,
            'size'      => $size,
            'more_data' => (count($more_data) < 1) ? 0 : 1 ,
        );
        return $return;
    }

    //获取区域下的基地列表
    public function branchList(){
        $id    = input('id',0,'intval');
        $where = array(
            'b_r_id'    => $id,
            'branch_id' => array('gt',0),
            'is_open'   => 1,
        );
        
        $list = db('Branch')
             -> field('branch_id,branch_name')
             -> where($where)
             -> order('is_top DESC,list_order ASC,branch_id DESC')
             -> select();
        $html = '<option value="0">请选择</option>';
        foreach ($list as $k => $v) {
            $html.= '<option value="'.$v['branch_id'].'">'.$v['branch_name'].'</option>';
        }

        exit($html);
    }

    //获取基地信息
    public function getBranch(){
        $id    = input('id',0,'intval');
        $data  = model('branch')
              -> field('branch_name,content,extend,longitude,latitude,address')
              -> find($id)
              -> toArray();

        $data['address']        = empty($data['address']) ? '' : $data['address'] ;
        // $data['extend']         = json_decode($data['extend'], true);
        $data['extend']['info'] = empty($data['extend']['info']) ? '' : $data['extend']['info'];
        
        return $data;
    }

    //制度指南文章详情
    public function detail(){
        
        $img = Db::name('img')->find(2);
        $this->assign('img',$img);

        $id = input('id', 0, 'intval');
        $articleId  = $this->request->param('id', 0, 'intval');
        $categoryId = $this->request->param('cid', 0, 'intval');
        $catId = $this->request->param('cat', 0, 'intval');


        $portalCategoryModel = new PortalCategoryModel();
        $category            = $portalCategoryModel->field('id,name')->where('parent_id', 0)->select();
        $this->assign('category', $category);

        //文章内容
        $postService         = new PostService();
        $article    = $postService->publishedArticle($articleId, $categoryId);

        if (empty($articleId)) {
            abort(404, '文章不存在!');
        }

        //访问量统计
        Db::name('portal_post')->where(['id' => $articleId])->setInc('post_hits');

        hook('portal_before_assign_article', $article);

        $this->assign('article', $article);

        //获取该文字的分类顶级ID
        /*$cat_id = Db::name('portal_category_post')->where(['post_id' => $articleId])->value('category_id');
        if ($cat_id != 0) {
            $cat_id = Db::name('portal_category')->where(['id' => $cat_id])->value('parent_id');
        }
        $this->assign('cat_id', $cat_id);*/


        return $this->fetch('/detail');
    }

}
